import { useUserStore } from '@/stores/user'

export default {
    install(app) {
        // 权限指令
        app.directive('permissions', {
            mounted: function (el, binding, vnode) {
                const userStore = useUserStore()
                // 获取用户信息
                if (!userStore.token?.TokenContent) {
                    el.parentNode && el.parentNode.removeChild(el)
                    return
                }
                // 获取配置权限
                const configPermissions = binding.value
                if (!(configPermissions instanceof Array)) {
                    throw new Error('v-permissions必须为一个数组')
                }

                // 验证权限
                if (
                    configPermissions.findIndex(
                        (code) =>
                            userStore.permissionBtnNameList.findIndex(
                                (p) => p === code
                            ) > -1
                    ) === -1
                ) {
                    el.parentNode && el.parentNode.removeChild(el)
                }
            }
        })
    }
}
